package com.hzx.controller;

import com.hzx.pojo.Order;
import com.hzx.pojo.User;
import com.hzx.service.*;
import com.hzx.until.FileUpload;
import com.hzx.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class PersonalCenterController {

    private static String HEADPICTUREPATH = "C:\\Users\\49975\\Desktop\\数据库\\DBPJ\\spring02\\src\\main\\resources\\static\\img\\UserHead\\" ;
    //private static String HEADPICTUREPATH = "F:\\newideafile\\dbpj\\src\\main\\resources\\static\\img\\UserHead\\" ;
    private static String HEADPIC = "\\img\\UserHead\\" ;

    @Autowired
    private UserInformationService userInformationService ;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private KindService kindService ;

    @Autowired
    private OrderService orderService ;

    @Autowired
    ItemService itemService ;

    @Autowired
    private HeadPictureService headPictureService ;

    @GetMapping("/personal-center")
    public String toPersonalCenter(HttpServletRequest request,Model model){
        Integer uid = (Integer) request.getSession().getAttribute("uid") ;
        if(StringUntil.isNullOrEmpty(uid)||uid==0){
            return "index" ;
        }

        //将user的信息返回
        User user = userInformationService.getUserByUid(uid) ;
        List kindList = kindService.getAllKind() ;

        List releaseList =  itemService.getAllItemsByUid(uid); // 通过用户的uid 查找对应发布的全部商品
        List demandList = itemService.getAllDemandByUid(uid);


        model.addAttribute("demands",demandList);
        model.addAttribute("items",releaseList) ;
        model.addAttribute("user",user) ;
        model.addAttribute("kindlist",kindList) ;
        return "personal-center" ;
    }


    //修改个人信息
    @RequestMapping(value = "/updateuser",method = RequestMethod.POST)
    public String updateUserInformation(HttpServletRequest request,Model model,
                                            @RequestParam("img") MultipartFile file,
                                            @RequestParam String name,
                                            @RequestParam char sex,
                                            @RequestParam String address,
                                            @RequestParam String phone ,
                                            @RequestParam String wechat ,
                                            @RequestParam String qq ,
                                            @RequestParam String password){

        Integer uid = (Integer) request.getSession().getAttribute("uid") ;
        User user = new User() ;
        user.setId(uid.longValue());

        if(!StringUntil.isNullOrEmpty(file)){
            String filename = uid+".jpg";
            user.setPictureUrl(HEADPIC+filename);
            FileUpload.File_upload(file,HEADPICTUREPATH,filename) ;
        }
        if(!StringUntil.isNullOrEmpty(name)){
            user.setName(name);
        }
        if(!StringUntil.isNullOrEmpty(password)){
            password = StringUntil.strEncrypt(password) ;
            user.setPassword(password);
        }
        if(StringUntil.isNullOrEmpty(sex)){
             user.setSex(sex);
        }
        if(!StringUntil.isNullOrEmpty(address)){
            user.setAddress(address);
        }
        if(!StringUntil.isNullOrEmpty(phone)){
            user.setPhone(phone);
        }
        if(!StringUntil.isNullOrEmpty(wechat)){
            user.setWechat(wechat);
        }
        if(!StringUntil.isNullOrEmpty(qq)){
            user.setQq(qq);
        }
        int ans = userInformationService.updateUserBySelective(user);
        if(ans==1){
            model.addAttribute(user) ;
            request.getSession().setAttribute("user",userInformationService.getUserByUid(uid));
            return "personal-center" ;
        }else {
            return "修改失败" ;
        }
    }

    //发布求购
    @RequestMapping(value = "postorder")
    @ResponseBody
    public String postCommodity(HttpServletRequest request ,
                                @RequestParam int kid ,
                                @RequestParam String find_name ,
                                @RequestParam String content){

        Order order =  new Order() ;
        order.setBtime(new Date(System.currentTimeMillis()));
        order.setKid(kid);
        order.setFind_name(find_name);
        int ans = orderService.insertBySelective(order) ;
        if(ans==1){
            return  "发布成功" ;
        }else{
            return "发布失败" ;
        }
    }
    //发布的商品

}
